package com.scut.edu.wanyou;


import com.scut.edu.wanyou.account.AccountActivity;
import com.scut.edu.wanyou.channel.ChannelActivity;
import com.scut.edu.wanyou.home.HomeActivity;
import com.scut.edu.wanyou.home.talk.XmppTool;
import com.scut.edu.wanyou.more.MoreActivity;
import com.scut.edu.wanyou.search.SearchActivity;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends TabActivity implements OnClickListener {
    public static String TAB_TAG_TALK = "talk";
    public static String TAB_TAG_CHANNEL = "channel";
    public static String TAB_TAG_ACCOUNT = "account";
    public static String TAB_TAG_SEARCH = "search";
    public static String TAB_TAB_MORE = "more";
    public static TabHost mTabHost;
    static final int COLOR1 = Color.parseColor("#787878");
    static final int COLOR2 = Color.parseColor("#ffffff");
    ImageView mBut1, mBut2, mBut3, mBut4, mBut5;
    TextView mCateText1, mCateText2, mCateText3, mCateText4, mCateText5;

    Intent mHomeItent, mChannelIntent, mSearchIntent, mAccountIntent,
            mMoreIntent;

    int mCurTabId = R.id.channel1;

    // Animation
    private Animation left_in, left_out;
    private Animation right_in, right_out;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        prepareAnim();
        prepareIntent();
        setupIntent();
        prepareView();
    }

    private void prepareAnim() {
        left_in = AnimationUtils.loadAnimation(this, R.anim.left_in);
        left_out = AnimationUtils.loadAnimation(this, R.anim.left_out);

        right_in = AnimationUtils.loadAnimation(this, R.anim.right_in);
        right_out = AnimationUtils.loadAnimation(this, R.anim.right_out);
    }

    private void prepareView() {
        mBut1 = (ImageView) findViewById(R.id.imageView1);
        mBut2 = (ImageView) findViewById(R.id.imageView2);
        mBut3 = (ImageView) findViewById(R.id.imageView3);
        mBut4 = (ImageView) findViewById(R.id.imageView4);
        findViewById(R.id.channel1).setOnClickListener(this);
        findViewById(R.id.channel2).setOnClickListener(this);
        findViewById(R.id.channel3).setOnClickListener(this);
        findViewById(R.id.channel4).setOnClickListener(this);
        mCateText1 = (TextView) findViewById(R.id.textView1);
        mCateText2 = (TextView) findViewById(R.id.textView2);
        mCateText3 = (TextView) findViewById(R.id.textView3);
        mCateText4 = (TextView) findViewById(R.id.textView4);
    }

    private void prepareIntent() {
        mChannelIntent = new Intent(this, ChannelActivity.class);
        mAccountIntent = new Intent(this, AccountActivity.class);
        mSearchIntent = new Intent(this, SearchActivity.class);
        mMoreIntent = new Intent(this, MoreActivity.class);
    }

    public boolean dispatchKeyEvent(KeyEvent event)
    {
        if (event.getAction() == KeyEvent.ACTION_DOWN&& event.getKeyCode() == KeyEvent.KEYCODE_BACK)
        {
        	if(mCurTabId==R.id.channel1)
        	{
        		Log.i("MainActivity", "退出");
        		showExitGameAlert();
        		return true;
        	}else {
				Log.i("MainActivity", "This is a test!");
				return false;
			}
    		
        }
        return super.dispatchKeyEvent(event);
    }
    private void setupIntent() {
        mTabHost = getTabHost();

        mTabHost.addTab(buildTabSpec(TAB_TAG_CHANNEL,
                R.string.category_channel, R.drawable.square, mChannelIntent));
        mTabHost.addTab(buildTabSpec(TAB_TAG_SEARCH, R.string.category_search,
                R.drawable.rating, mSearchIntent));
        mTabHost.addTab(buildTabSpec(TAB_TAG_ACCOUNT,
                R.string.category_account, R.drawable.social, mAccountIntent));
        mTabHost.addTab(buildTabSpec(TAB_TAB_MORE, R.string.category_more,
                R.drawable.personal, mMoreIntent));
    }

    private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,
                                         final Intent content) {
        return mTabHost
                .newTabSpec(tag)
                .setIndicator(getString(resLabel),
                        getResources().getDrawable(resIcon))
                .setContent(content);
    }

    public static void setCurrentTabByTag(String tab) {
        mTabHost.setCurrentTabByTag(tab);
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (mCurTabId == v.getId()) {
            return;
        }
        mBut1.setImageResource(R.drawable.square);
        mBut2.setImageResource(R.drawable.rating);
        mBut3.setImageResource(R.drawable.social);
        mBut4.setImageResource(R.drawable.personal);
        mCateText1.setTextColor(COLOR1);
        mCateText2.setTextColor(COLOR1);
        mCateText3.setTextColor(COLOR1);
        mCateText4.setTextColor(COLOR1);
        int checkedId = v.getId();
        final boolean o;
        if (mCurTabId < checkedId)
            o = true;
        else
            o = false;
        if (o)
            mTabHost.getCurrentView().startAnimation(left_out);
        else
            mTabHost.getCurrentView().startAnimation(right_out);
        switch (checkedId) {
            case R.id.channel1:
                mTabHost.setCurrentTabByTag(TAB_TAG_CHANNEL);
                mBut1.setImageResource(R.drawable.square);
                mCateText1.setTextColor(COLOR2);
                break;
            case R.id.channel2:
                mTabHost.setCurrentTabByTag(TAB_TAG_SEARCH);
                mBut2.setImageResource(R.drawable.rating);
                mCateText2.setTextColor(COLOR2);
                break;
            case R.id.channel3:
                mTabHost.setCurrentTabByTag(TAB_TAG_ACCOUNT);
                mBut3.setImageResource(R.drawable.social);
                mCateText3.setTextColor(COLOR2);
                break;
            case R.id.channel4:
                mTabHost.setCurrentTabByTag(TAB_TAB_MORE);
                mBut4.setImageResource(R.drawable.personal);
                mCateText4.setTextColor(COLOR2);
                break;
            default:
                break;
        }

        if (o)
            mTabHost.getCurrentView().startAnimation(left_in);
        else
            mTabHost.getCurrentView().startAnimation(right_in);
        mCurTabId = checkedId;
    }

    private void showExitGameAlert() {
        AlertDialog.Builder builder = new Builder(MainActivity.this);
        builder.setMessage("确定要退出吗?");
        builder.setTitle("提示");
        builder.setPositiveButton("确认",
                new android.content.DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        android.os.Process.killProcess(android.os.Process.myPid());
                    }
                });
        builder.setNegativeButton("取消",
                new android.content.DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        builder.create().show();

    }
}